﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Traceable.App.Application.Command.Goods;
using Traceable.App.DTOS;
using Traceable.ErrorCode;

namespace Traceable.App.Controllers
{
    /// <summary>
    /// 商品溯源基本信息服务
    /// </summary>
    [Route("api/[controller][action]")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        IMediator _mediator;

        public GoodsController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 获取商品名称
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiResult<List<GoodsDto>>> GetGoods([FromQuery]GetGoodsCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 商品溯源基本信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiPaging<GoodsTraceableDto>>GetTraceablelist([FromQuery]GoodsTraceableListCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 溯源基本信息新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public Task<ApiResult<int>> CreateGoodsTraceable(CreateGoodsTraceCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 删除溯源基本信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public Task<ApiResult<int>> DeleteGoodsTraceable(DeleteGoodsTraceCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<string>>UpImgAsync(IFormFile file)
        {
            var result = new ApiResult<string>();
            var p =Directory.GetCurrentDirectory()+@"/wwwroot/Pic/"+file.FileName;
            using (FileStream fs = new FileStream(p,FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            result.Code = StatusEnum.OK;
            result.Data= "http://localhost:5253/Pic/" + file.FileName;
            return result;
        }
        /// <summary>
        /// 溯源基本信息修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<ApiResult<int>> UpdateGoodsTraceable(UpdateGoodsTraceCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 获取商品详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<GoodsTraceableDto>> GetGoodsDetail([FromQuery]GetGoodsDetailCommand request)
        {
            return _mediator.Send(request);
        }
    }
}
